--[[-------------------------------------------------
Exec.lua
Version: 1.2.2
Author: HSolo, mozers
---------------------------------------------------
Calculates the selected text as a mathematical expression
or opening the browser selected URL
http://forum.ru-board.com/topic.cgi?forum=5&topic=3215&start=2020#3
--]]-------------------------------------------------

local function FindExpression(str)
	local patternNum = "([\-\+\*\/%b()%s]*%d+[\.\,]*%d*[\)]*)"
	local startPos, endPos, number, formula
	startPos = 1
	formula = ''
	while true do
		startPos, endPos, number = str:find(patternNum, startPos) -- Find the numbers, signs, brackets (that is, all that can be taken as part of the formula)
		if startPos == nil then break end
		startPos = endPos + 1
		number = number:gsub('%s+', '')                           -- no space
		number = number:gsub('^([\(%d]+)', '+%1')                 -- Where there is no sign in front of it, put a "+" (ie, spaces and line breaks are replaced by "+")
		number = number:gsub('^([\)]+)([%d]+)', '%1+%2')          -- followed by the "+" (in his absence) between the number and bracket
		formula = formula..number                                 -- Merge again transformed string
	end
	formula = formula:gsub('^[\+]', '')                      -- Early return extra "+" - kill it
	formula = formula:gsub("[\,]+",'.')                      -- Let's not hard on the character - decimal separator :)
	formula = formula:gsub("([\+])([\+]+)",'%1')             -- Remove the dual signs (+ +) = (+)
	formula = formula:gsub("([\-])([\+]+)",'%1')             -- Remove the double dash (- +) = (-)
	formula = formula:gsub("([\+\-\*\/])([\*\/]+)",'%1')     -- Remove the dual signs before * and / as this is a clear herd
	formula = formula:gsub("([\+\-\*\/])([\*\/]+)",'%1')     -- For conscience 'sake did twice
	formula = formula:gsub("([%d\)]+)([\+\*\/\-])",'%1 %2 ') -- Share of space
	return formula
end

local str = ''
if editor.Focus then
	str = editor:GetSelText()
else
	str = props['CurrentSelection']
end
if (str == '') then
	str = editor:GetCurLine()
end
if (#str < 3) then return end

if str:find('https?://(.*)') then
	shell.exec(str)
else
	if str:find("(math\.%w+)") then  -- In the case of complex mathematical expressions, formatting leave the user
		str = str:gsub("[=]",'')
	else
		str = FindExpression(str)
	end
	local result = loadstring('return '..str)()

	print('-> '..scite.GetTranslation("Calculate Expression")..': '..str)
	print('>> '..scite.GetTranslation("Result")..': '..result)

	--[[ -------- insert result to text ------
	editor:LineEnd() 
	local sel_start = editor.SelectionStart + 1
	local sel_end = sel_start + string.len(result)
	editor:AddText('\n= '..result)
	editor:SetSel(sel_start, sel_end+2)
	--]] -------------------------------------
end

--[[-------------------------------------------------
Tests like :)
1/2 56/4 - 56 (8-6)*4  4,5*(1+2)    66
3/6 6.4/2 6  (7-6)*4  45/4.1 66

dmfdmk v15*6dmd.ks skm4.37/3d(k)gm/sk+d skdmg(6,7+6)skdmgk

Колбаса = 24.5кг. * 120руб./кг
Бензин(ABC) = (2500км. / (11,5л./100км.)) * 18.4руб./л + Канистра =100руб.
Штукатурка = 22.4 м2 /80руб./100 м2

http://scite-ru.org
--]]-------------------------------------------------
